package com.jolink.omiis.wechat;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * SpringContextUtil 上下文，获取服务接口
 * @author WangYanqing
 *
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

	public SpringContextUtil() {
	}

	/**
	 * Spring应用上下文环境
	 */
	@Autowired
	private static ApplicationContext applicationContext;

	/**
	 * 实现ApplicationContextAware接口的回调方法，上下文环境
	 * 
	 * @param applicationContext
	 */
	@SuppressWarnings("static-access")
	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	/**
	 * @return ApplicationContext
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * 获取对象
	 * 
	 * @param name
	 * @return Object
	 * @throws BeansException
	 */
	public static Object getBean(String name) throws BeansException {
		return applicationContext.getBean(name);
	}

	/**
	 * 获取对象
	 * 
	 * @param name
	 * @return Object
	 * @throws BeansException
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class<?> clazz) throws BeansException {
		return (T) applicationContext.getBean(clazz);
	}

}